# IIS Deployment Readiness Check
# Run this script to verify your system is ready for IIS deployment

Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "  IIS Deployment Readiness Check" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""

$passCount = 0
$failCount = 0

function Check-Item {
    param(
        [string]$Name,
        [bool]$Result,
        [string]$Help = ""
    )
    
    Write-Host "Checking: $Name " -ForegroundColor Yellow -NoNewline
    
    if ($Result) {
        Write-Host "[PASS]" -ForegroundColor Green
        $script:passCount++
    } else {
        Write-Host "[FAIL]" -ForegroundColor Red
        if ($Help) {
            Write-Host "  -> $Help" -ForegroundColor Gray
        }
        $script:failCount++
    }
}

# System Checks
Write-Host "1. System Environment" -ForegroundColor Cyan
Write-Host "--------------------" -ForegroundColor Cyan

$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
Check-Item "Administrator Rights" $isAdmin "Run PowerShell as Administrator"

# Node.js Checks
Write-Host ""
Write-Host "2. Node.js Environment" -ForegroundColor Cyan
Write-Host "--------------------" -ForegroundColor Cyan

$nodeExists = $null -ne (Get-Command node -ErrorAction SilentlyContinue)
Check-Item "Node.js Installed" $nodeExists "Download from https://nodejs.org"

if ($nodeExists) {
    $nodeVersion = node --version
    Write-Host "  Version: $nodeVersion" -ForegroundColor Gray
}

$npmExists = $null -ne (Get-Command npm -ErrorAction SilentlyContinue)
Check-Item "npm Available" $npmExists "Should be installed with Node.js"

# IIS Checks
Write-Host ""
Write-Host "3. IIS Components" -ForegroundColor Cyan
Write-Host "--------------------" -ForegroundColor Cyan

$iisInstalled = $null -ne (Get-Service W3SVC -ErrorAction SilentlyContinue)
Check-Item "IIS Installed" $iisInstalled "Install IIS via Server Manager or Windows Features"

if ($iisInstalled) {
    $urlRewriteInstalled = Test-Path "HKLM:\SOFTWARE\Microsoft\IIS Extensions\URL Rewrite"
    Check-Item "URL Rewrite Module" $urlRewriteInstalled "Download: https://www.iis.net/downloads/microsoft/url-rewrite"
    
    $iisnodeInstalled = (Test-Path "${env:ProgramFiles}\iisnode") -or (Test-Path "${env:ProgramFiles(x86)}\iisnode")
    Check-Item "iisnode Module" $iisnodeInstalled "Download: https://github.com/Azure/iisnode/releases"
}

# Project Checks
Write-Host ""
Write-Host "4. Project Files" -ForegroundColor Cyan
Write-Host "--------------------" -ForegroundColor Cyan

$projectRoot = $PSScriptRoot

$frontendDeps = Test-Path (Join-Path $projectRoot "node_modules")
Check-Item "Frontend Dependencies" $frontendDeps "Run: npm install"

$backendDeps = Test-Path (Join-Path $projectRoot "server\node_modules")
Check-Item "Backend Dependencies" $backendDeps "Run: cd server; npm install"

$frontendConfig = Test-Path (Join-Path $projectRoot "public\web.config")
Check-Item "Frontend web.config" $frontendConfig "File missing"

$backendConfig = Test-Path (Join-Path $projectRoot "server\web.config")
Check-Item "Backend web.config" $backendConfig "File missing"

$distExists = Test-Path (Join-Path $projectRoot "dist")
$serverDistExists = Test-Path (Join-Path $projectRoot "server\dist")
$built = $distExists -and $serverDistExists
Check-Item "Project Built" $built "Run: .\build-all.bat"

# Summary
Write-Host ""
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host "Summary" -ForegroundColor Cyan
Write-Host "=====================================" -ForegroundColor Cyan
Write-Host ""

$total = $passCount + $failCount
Write-Host "Total Checks: $total" -ForegroundColor White
Write-Host "Passed: $passCount" -ForegroundColor Green
Write-Host "Failed: $failCount" -ForegroundColor Red
Write-Host ""

if ($failCount -eq 0) {
    Write-Host "SUCCESS! Ready for deployment!" -ForegroundColor Green
    Write-Host ""
    Write-Host "Next Steps:" -ForegroundColor Cyan
    Write-Host "  1. Build project: .\build-all.bat" -ForegroundColor White
    Write-Host "  2. Deploy to IIS: .\deploy-iis-quick.ps1 (as Admin)" -ForegroundColor White
    Write-Host "  3. Read guide: IIS部署快速指南.md" -ForegroundColor White
} else {
    Write-Host "WARNING: $failCount check(s) failed. Please fix them first." -ForegroundColor Yellow
    Write-Host ""
    Write-Host "Help Resources:" -ForegroundColor Cyan
    Write-Host "  - Quick Guide: IIS部署快速指南.md" -ForegroundColor White
    Write-Host "  - Full Docs: IIS_DEPLOY.md" -ForegroundColor White
}

Write-Host ""
